package com.example.permission.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.permission.common.CommonParam;
import com.example.permission.common.page.Page;
import com.example.permission.entity.dto.SysDeptDTO;
import com.example.permission.entity.dto.page.SysDeptPageDTO;
import com.example.permission.entity.po.SysDept;
import com.example.permission.entity.vo.SysDeptTreeVO;
import com.example.permission.enums.DataStatusEnum;
import com.example.permission.mapper.SysDeptMapper;
import com.example.permission.service.SysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息(SysDept)表服务实现类
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    public SysDept insert(SysDeptDTO sysDeptDTO) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptDTO, sysDept);
        sysDept.setCreateTime(new Date());
        sysDept.setCreator(CommonParam.getInstance().getUserNo());
        this.sysDeptMapper.insert(sysDept);
        return sysDept;
    }

    @Override
    public SysDept update(SysDeptDTO sysDeptDTO) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptDTO, sysDept);
        sysDept.setUpdateTime(new Date());
        sysDept.setUpdator(CommonParam.getInstance().getUserNo());
        this.sysDeptMapper.update(sysDept);
        return this.queryById(sysDept.getDeptId());
    }

    @Override
    public boolean deleteById(Integer deptId) {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(deptId);
        sysDept.setDataStatus(DataStatusEnum.ABNORMAL.getCode());
        return this.sysDeptMapper.update(sysDept) > 0;
    }

    @Override
    public SysDept queryById(Integer deptId) {
        return this.sysDeptMapper.queryById(deptId);
    }

    @Override
    public Page<SysDept> queryListByPage(SysDeptPageDTO sysDeptPageDTO) {
        int count = this.sysDeptMapper.queryCountByPage(sysDeptPageDTO);
        List<SysDept> sysDeptList = this.sysDeptMapper.queryListByPage(sysDeptPageDTO);
        return new Page<>(count, sysDeptList);
    }

    /**
     * 递归树
     *
     * @param sysDeptTreeVOList
     * @param pId
     * @return
     */
    private static List<SysDeptTreeVO> makeTree(List<SysDeptTreeVO> sysDeptTreeVOList, Integer pId) {
        //子类
        List<SysDeptTreeVO> children = sysDeptTreeVOList.stream()
                .filter(x -> x.getPid().equals(pId)).collect(Collectors.toList());
        //后辈中的非子类
        List<SysDeptTreeVO> successor = sysDeptTreeVOList.stream()
                .filter(x -> !x.getPid().equals(pId)).collect(Collectors.toList());
        children.forEach(x -> makeTree(successor, x.getDeptId()).forEach(y -> x.getChildren().add(y)));
        return children;
    }

    @Override
    public List<SysDept> queryDeptTree(Integer pid) {
        SysDept sysDept = new SysDept();
        sysDept.setPid(pid);
        sysDept.setDataStatus(DataStatusEnum.NORMAL.getCode());
        List<SysDept> sysDeptList = sysDeptMapper.queryAll(sysDept);
        return sysDeptList;
    }

    @Override
    public List<SysDeptTreeVO> queryDeptTree() {
        SysDept sysDept = new SysDept();
        sysDept.setDataStatus(DataStatusEnum.NORMAL.getCode());
        List<SysDept> sysDeptList = sysDeptMapper.queryAll(sysDept);
        List<SysDeptTreeVO> sysDeptTreeVOList = JSON.parseArray(JSON.toJSONString(sysDeptList), SysDeptTreeVO.class);
        return makeTree(sysDeptTreeVOList, -1);
    }

}